package com.lagou.servlet;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lagou.factory.BeanFactory;
import com.lagou.factory.ProxyFactory;
import com.lagou.log.MLog;
import com.lagou.pojo.Result;
import com.lagou.scan.AnnotionScanner;
import com.lagou.service.AccountService;
import com.lagou.service.AccountServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.HashMap;

/**
 * @Authther: mrwu
 * @date:2021/3/12-03
 * @Description:com.lagou.servlet
 */
@WebServlet(urlPatterns = "/transferServlet")
public class TransferServlet extends HttpServlet {
    //初始化service对象
//    private AccountService accountService= (AccountService)BeanFactory.getBean("accountService");
     //从代理工厂获取代理对象(单例模式下)
//    private AccountService accountService = (AccountService)ProxyFactory.getInstance().getJDKProxy(BeanFactory.getBean("accountService"));
//    private ProxyFactory proxyFactory=(ProxyFactory)BeanFactory.getBean("proxyFactory");
//    private AccountService accountService=(AccountService)proxyFactory.getJDKProxy(BeanFactory.getBean("accountServiceImpl"));


        private AccountService accountService;
    //为了取消掉new 使用反射根据类名生成对应的实例 ，然后根据反射
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //将get指向post处理
        doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求字符编码
        req.setCharacterEncoding("UTF-8");
        //获取请求参数
        String fromcardNo = req.getParameter("fromCardNo");
        String tocardNo = req.getParameter("tocardNo");
        String moneys = req.getParameter("money");
        int money = Integer.parseInt(moneys);
        Result result = new Result();
             try {
            accountService.transfer(fromcardNo, tocardNo, money);
            result.setStatus("200");
            result.setMassage("成功");
            resp.getWriter().write(result.toString());
            resp.getWriter().flush();
        }catch (Exception e){
            result.setStatus("201");
            result.setMassage(e.toString());
            resp.getWriter().write(result.toString());
            resp.getWriter().flush();
        }

    }


    @Override
    public void init() throws ServletException {
//         ProxyFactory proxyFactory=(ProxyFactory)BeanFactory.getBean("proxyFactory");
//         Object accountServiceImpl = BeanFactory.getBean("accountServiceImpl");
//         accountService=(AccountService)proxyFactory.getJDKProxy(accountServiceImpl);//用代理对象事务才能生效
        //****声明式事务下---注解模式下---
        accountService=(AccountService) BeanFactory.getBean("accountServiceImpl");

    }
}
